package org.jspar.route;

public class LocalRoute {

	/** Find the overlapping region between (x1,x2) and (X1, X2).
	 * If the input regions don't overlap return the region between them.
	 * @param x1
	 * @param x2
	 * @param X1
	 * @param X2
	 * @return
	 */
	public static int[] findIntersection(int x1, int x2, int X1, int X2) {
		boolean xEndsFirst = (x2 < X2);
		boolean xStartsFirst = (x1 < X1);
		int[] res = new int[2];
		if (x2 < X1 || x1 > X2) {
			// No Overlap case
			if (xEndsFirst && xStartsFirst) {
				res[0] = X1;
				res[1] = x2;
			} else {
				res[0] = x1;
				res[1] = X2;
			}
			return res; // false
		}
		if (xEndsFirst)
			res[1] = x2;
		else
			res[1] = X2;

		if (xStartsFirst)
			res[0] = X1;
		else
			res[0] = x1;

		return res; // true
	}
}
